package com.edge.pbx.view.gain;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.edge.pbx.view.PbxCircleSeekBar;
import com.hanlink.dsp.util.Constants;
import com.qztech.utils.YzDebug;

public class PbxGainContentLayout extends ViewGroup {
    private float mCenterWidthPercent = 0.1f;
    public PbxGainContentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
        View view = null;
        view = getChildAt(0);

        if (view != null){
            view.measure(MeasureSpec.makeMeasureSpec((int) (width*mCenterWidthPercent), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec((int) (height),MeasureSpec.EXACTLY));
        }
        int halfSeekBarLayoutWidth = (int) ((1f - mCenterWidthPercent)*width/2f);
        int seekBarSize = (int) Math.min(height/2.8f,halfSeekBarLayoutWidth/2.7f);
        int spec = MeasureSpec.makeMeasureSpec(seekBarSize, MeasureSpec.EXACTLY);
        for (int i = 0; i < 8; i++) {
            view = getChildAt(i+1);
            if (view == null) continue;
            view.measure(spec, spec);

        }

        int w = (int) (seekBarSize*0.47f);
        int h = (int) (0.324f * w);
        spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
        int spech = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
        for (int i = 0; i < 8; i++) {//linked
            view = getChildAt(i+9);
            if (view == null) continue;
            view.measure(spec, spech);

        }

        w = (int) (seekBarSize * 0.42f);
        h = (int) (w * 0.536f);
        spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY);
        spech = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
        for (int i = 0; i < 16; i++) {//mute and degress
            view = getChildAt(i+17);
            if (view == null) continue;
            view.measure(spec, spech);

        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;
        View view = null;
        view = getChildAt(0);
        int vw, vh ;

        if (view != null){
            vw = view.getMeasuredWidth();
            vh = view.getMeasuredHeight();
            l = (width - vw) >> 1;
            t = (height - vh) >> 1;
            view.layout(l, t, l+vw, t +vh);
        }
        int halfSeekBarLayoutWidth = (int) ((1f - mCenterWidthPercent)*width/2f);
        view = getChildAt(1);
        vw = view.getMeasuredWidth();
        vh = view.getMeasuredHeight();
        int hPadding = (halfSeekBarLayoutWidth - vw*2)/3;
        int vPadding = (height - vh*2) /5;
        int cw, ch,cl,ct;
        int tmp = (int) ((1 + mCenterWidthPercent)*width/2);
        for (int i = 0; i < 8; i++) {
            view = getChildAt(i+1);

            t = vPadding + (vh + vPadding*2)*(i%2);
            if (i < 4){
                l =  hPadding + (vw + hPadding)*(i/2);
            }else {
                l = tmp + hPadding + (vw + hPadding)*(i/2 - 2);
            }
            if (view != null)
                view.layout(l, t, l + vw, t + vh);

            //linked
            view = getChildAt(i + 9);
            if (view != null){
                cw = view.getMeasuredWidth();
                ch = view.getMeasuredHeight();
                cl = l + ((vw - cw) >> 1);
                ct = t + (int) (vh * 0.8f)-ch/2;
                view.layout(cl, ct, cl+cw, ct+ch);
            }
            //muted
            view = getChildAt(i + 17);
            if (view != null){
                cw = view.getMeasuredWidth();
                ch = view.getMeasuredHeight();
                cl = (int) (l + (vw*0.45f - cw));
                ct = t + (int) (vh * 1.05f);
                view.layout(cl, ct, cl+cw, ct+ch);
            }
            //
            view = getChildAt(i + 25);
            if (view != null){
                cw = view.getMeasuredWidth();
                ch = view.getMeasuredHeight();
                cl = (int) (l + vw*0.55f);
                ct = t + (int) (vh * 1.05f);
                view.layout(cl, ct, cl+cw, ct+ch);
            }
        }
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View view = null;
        final String fmt = "%ddB";
        final PbxCircleSeekBar.OnCircleSeekBarTextListener textListener = value ->{
//            YzDebug.d("Yang","onFinishInflate:->"+value);
            if (value == 0){
                return "Off";
            }
            return String.format(fmt, value- Constants.MAX_VOL);
        };
        for (int i = 0, count = getChildCount(); i < count;  i++){
            view = getChildAt(i);
//            YzDebug.d("Yang",i+"addaaaaaaaaa:->"+(view instanceof PbxCircleSeekBar)
//            +" "+ view.getClass().getName());
            if(view instanceof PbxCircleSeekBar){
                ((PbxCircleSeekBar)view).setOnCircleSeekBarTextListener(textListener);
            }


        }
    }
}
